// 全局请求封装
// const base_url = 'https://test.asiatop.net/api-vouchery-app/api/v1'
const base_url = 'https://test.asiatop.net/api-ry-app/api/v1'
// 请求超出时间
const timeout = 5000

export default (params) => {

	let url = params.url;
	let method = params.method || "get";
	let data = params.data || {};

	return new Promise((resolve, reject) => {
		// let token = uni.getStorageSync('token')
		uni.request({
			url: base_url + url,
			method: method,
			// 把token带到所有请求头里面
			header: params.header ? {
				'Authorization': uni.getStorageSync('token') ? uni.getStorageSync('token') : '111',
				...params.header
			} : {'Authorization': uni.getStorageSync('token') ? uni.getStorageSync('token') : '111'},
			data: params.data ? params.data : {},
			timeout,

			success(response) {
				// 获取到token，且使用setStorageSync存进本地
				let datatoken =	response.header['x-mall-token']? response.header['x-mall-token']:''
				if(datatoken != ''){
					uni.setStorageSync('token', datatoken);
				}
				if (response.data.code !== 200) {
					uni.showToast({
						icon: "none",
						duration: 4000,
						title: response.data.msg
					});
				}
				console.log(response.data)
				resolve(response.data);
			},

			fail(err) {
				uni.showToast({
					icon: "none",
					title: '服务响应失败'
				});
				console.error(err);
				reject(err);
			},

			complete() {
				uni.hideLoading();
			}

		});
	}).catch((err) => {
		console.log('打印失败', err);
	});

}